package com.ck.core.util;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author linqiang
 * @version v1.0
 * @Description 爬虫工具
 * @date 2019-05-21 17:25
 */
public class SpiderUtils {
    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 根据url获取网页title
     *
     * @param url 网页地址
     * @return 网页title
     */
    public static String getTitleByUrl(String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        }
        if (null == doc) {
            return null;
        }
        return doc.title();
    }
}
